
#include "main.h"

//
unsigned char SIM_Watch_Cycle = 100;
unsigned char SIM_Read;

/* void I2C_Init( void ) */
/* { */
/* 	if (  */

/* } */
 
void IMSI_Range_Init( void )
{
/* 	unsigned int pIMSI_Range; */
/* 	IMSI_RANGE Range; */

/* 	if ( Ex_LoadFrom((unsigned char*)&Range, EX_ADDR_IMSI_RANGES, sizeof(IMSI_RANGE)) ) */
/* 	{ */
/* 		if ( Range.Start[0] == 0xFF )	/\* if IMSI range was not seted, then set a full open IMSI range *\/ */
/* 		{ */
/* 			MemSet(Range.Start, 8, 0x00 ); */
/* 			Range.Start[0] = 0xF0; */
/* 			MemSet(Range.Stop, 8, 0x99 ); */
/* 			Range.Stop[0] = 0xF9; */
			
/* 			if ( Ex_SaveAndCheck(EX_ADDR_IMSI_RANGES, (unsigned char*)&Range, sizeof(IMSI_RANGE) ) == FALSE )  */
/* 			{ */
/* 				goto ERR; */
/* 			}  */

/* 			pIMSI_Range = EX_ADDR_IMSI_RANGES + sizeof(IMSI_RANGE); */

/* 			if ( Ex_SaveAndCheck(EX_ADDR_P_IMSI_RANGE, (unsigned char*)&pIMSI_Range, sizeof(pIMSI_Range)) == FALSE )  */
/* 			{ */
/* 				goto ERR; */
/* 			} */
/* 		} */
		
/* 		return ; */
/* 	} */

/*   ERR: */
/* /\* 	lcd_print_str(STR__EX_EEPROM_ERRROR, 0, 1, 1); *\/ */
/* 	delay_ms(1000); */
/* 	INT0IF = 1; 			  /\* To Save Mode *\/ */
} 
 
void IMSI_Range_Clear_All( void )
{
/* 	unsigned int pIMSI_Range = EX_ADDR_IMSI_RANGES; */
/* 	Ex_MemSet ( EX_ADDR_IMSI_RANGES, 0xFF, IMSI_RANGES_INDEX_MAX * sizeof(IMSI_RANGE) ); */
/* 	Ex_SaveAndCheck(EX_ADDR_P_IMSI_RANGE, (unsigned char*)&pIMSI_Range, sizeof(pIMSI_Range) ); */
}
 
unsigned char IMSI_Valid ( unsigned char* IMSI_Str  )
{
/* 	IMSI_RANGE Range; */
/* 	unsigned char Testable_IMSI[8]; */
/* 	unsigned int p = EX_ADDR_IMSI_RANGES; */

/* 	StrToBCD( Testable_IMSI, 8 , IMSI_Str); */

/*   NEXT_RANGE: */

/* 	Ex_LoadFrom((unsigned char*)&Range, p, sizeof(IMSI_RANGE) ); */
	
/* 	if ( Range.Start[0] == 0xFF ) */
/* 		return FALSE; */

/* 	if ( MemCmp(Testable_IMSI, Range.Start, 8) >= 0 && MemCmp(Testable_IMSI, Range.Stop, 8) <= 0 ) */
/* 	{ */
/* 		return TRUE; */
/* 	} */
	
/* 	p += sizeof(IMSI_RANGE); */
	
/* 	if ( p >= IMSI_RANGES_ADDRESS_MAX ) */
/* 		return FALSE; */
	
/* 	goto NEXT_RANGE; */
	return 0;
}

void SIM_Watch( void )
{
	unsigned char Response;
	
	static unsigned char ErrCnt = 2;

	SIM_Read = 0;

	if ( SIM_Watch_Cycle )
	{
		if ( Time_100ms )
			--SIM_Watch_Cycle;

		return;
	}
	

/* 	//if ( SysState == ENTER_PIN_PASSWORD || Installer_Config.Byte ) */
/* 	if ( SysState == ENTER_PIN_PASSWORD || Installer_Config.Byte ) */
/* 	{ */
/* 		ErrCnt = 3;					  /\* wait for PIN NUMBER entering  *\/ */
/* 		SIM_Watch_Cycle = 80; */
/* 		return; */
/* 	} */

/* 	/\* Check INTERNAL SIM *\/ */
/* 	if ( SIM_CARD == EXTERNAL && RegistrationStatus.REGISTERED )  */
/* 	{ */
/* 		PIN_EXTERNAL_SIM = 1; */
/* 		PIN_EXTERNAL_SIM_DIR = OUTPUT; */
/* 		NOP(); */
/* 		NOP(); */
/* 		if( PIN_INTERNAL_SIM && PIN_INTERNAL_SIM ) */
/* 		{ */
/* 			INT0IF = 1; */
/* 		} */
/* 		else */
/* 		{ */
/* 			PIN_EXTERNAL_SIM_DIR = INPUT; */
/* 		} */
/* 	} */
	
	//+CIMI: 425020210534859
	Response = SendToModem( "AT+CIMI\r", STR__OK , BUSY_SIM_WATCH);
	if ( Response != BUSY )
	{
		//puts(ModemRecBuffer, AUX_PORT);

		if ( Response == FALSE  )
		{
			if ( --ErrCnt == 0 )
			{
				//ErrCnt = 2;
/* 				lcd_print_str(STR______ERROR____, 0,1,1); */
/* 				lcd_print_str(STR__SIM_CARD_NOT_FOUND_, 0,2,0); */
//				BEEP();
/* 				delay_ms(100); */
/* 				BEEP_ERROR(); */
/* 				delay_ms(2000); */

/* 				INT0IF = 1; 				  /\* To Save Mode *\/ */
				RESET();
			}
		}
		else
#ifndef MODEM_TYPE__TELIT
			if ( !StrCmp("+CIMI", ModemRecBuffer) || errno == 5 )
#endif	
			{
				ErrCnt = 3;

#ifndef   MODEM_TYPE__TELIT
				StrCpy(Active_IMSI, &ModemRecBuffer[7] );
#else
				StrCpy(Active_IMSI, ModemRecBuffer );	
#endif
				/*  */
				//lcd_print_str(Active_IMSI,2,2,0);

/* 			if ( E2_StrCmp(Active_IMSI, E2_ADDR_MY_IMSI_NUMBER) )	/\* Check if internal SIM was changed *\/ */
/* 			{ */
/* 				lcd_print_str("FAIL ", 0,1,0); */
/* 				E2_Str_LoadFrom(Active_IMSI, E2_ADDR_MY_IMSI_NUMBER); */
/* 				lcd_puts(Active_IMSI);  */
/* 				StrCpy(Active_IMSI, &ModemRecBuffer[7] ); */
/* 				delay_ms(3000); */
/* 			} */
				/*  */
			
				//delay_ms(500);
				//if ( IMSI_Valid(&ModemRecBuffer[7]) == FALSE )
				if ( IMSI_Valid(Active_IMSI) == FALSE )
				{
					if ( SysState == CONNECTED_VOICE || SysState == INCOMING_CALL || 
						  SysState == CONVERSATION || SysState == CALLING )
					{
						SysState = MODEM_DISCONNECT;
					}
					else
					{
						SysState = EXT_SIM_NOT_VALID;
					}
				}
				else
				{
					SIM_Read = OK;
				
					if ( e2_Read( E2_ADDR_MY_IMSI_NUMBER) == 0xFF )
					{
						E2_Delete( E2_ADDR_MY_PHONE_NUMBER, CALL_NUMBER_SIZE );
						E2_Str_SaveTo ( E2_ADDR_MY_IMSI_NUMBER, Active_IMSI );
					
/* 					ClearBuffer(Active_IMSI, 16); */
/* 					E2_Str_LoadFrom(Active_IMSI, E2_ADDR_MY_IMSI_NUMBER ); */
/* 					lcd_print_str(Active_IMSI, 2,2,0); */
	
						//delay_ms(3000);
					}
	
					if ( E2_StrCmp(Active_IMSI, E2_ADDR_MY_IMSI_NUMBER) )	/* Check if internal SIM was changed */
					{
						if ( e2_Read(E2_ADDR_MY_PHONE_NUMBER) != 0xFF )
							E2_Delete( E2_ADDR_MY_PHONE_NUMBER, CALL_NUMBER_SIZE );

						if ( SysState != MANUFACTURE_TEST && ConfigFile.CFG__DISABLE_APPARATUS_IF_SIM_CHANGED == TRUE )
						{
							//if ( SysState != MENU && SysState != ENTER_PASSWORD && Installer_Config.Byte == 0 )
							SysState = INT_SIM_WAS_CHANGED;
						}
						else
						{
							E2_Str_SaveTo ( E2_ADDR_MY_IMSI_NUMBER, Active_IMSI );
							Config_Read();	/* Set CFG__DISABLE_APPARATUS_IF_SIM_CHANGED = TRUE  */
						}
					}
					else
					{
						if ( SysState == INT_SIM_WAS_CHANGED )
						{
							StatusBits.STAT__SIM_WAS_CHANGED = FALSE;
							SysState = IDLE;
						}
					}

					if ( SysState == EXT_SIM_NOT_VALID )
					{
						SysState = IDLE;
					}
				}
			}
		
		if ( SysState == MANUFACTURE_TEST )
			SIM_Watch_Cycle = 20;
		else
			SIM_Watch_Cycle = 60;
	}
}
 
unsigned char IMSI_Range_Find( IMSI_RANGE* Range_Testable )
{
/* 	unsigned int Address; */
/* 	IMSI_RANGE Range_Tabular; */

/* 	for ( Address = EX_ADDR_IMSI_RANGES; Address < IMSI_RANGES_ADDRESS_MAX; Address +=sizeof(IMSI_RANGE) ) */
/* 	{ */
/* 		Ex_LoadFrom((unsigned char*)&Range_Tabular, Address, sizeof(IMSI_RANGE) ); */
		
/* 		if ( !MemCmp(Range_Tabular.Start, Range_Testable->Start, 8) && !MemCmp(Range_Tabular.Stop, Range_Testable->Stop, 8) ) */
/* 			return TRUE; */
/* 	} */
	
	return FALSE;
}
